<?php 
class IssueDao extends Dao {

    function __construct() {
        parent::__construct();
        $this->table = 'issue';
        
    }

    public function insertObject($obj){
            $result = DB::insert(DB::raw("INSERT INTO issue (name, priority_id, task_id, status_id, created_by, created_date) ".
                    "VALUES ('".$obj->getName()."' ".
                            ", '".$obj->getPriority()->getId()."' ".
                            ", '".$obj->getTask()->getId()."' ".
                            ", '".$obj->getStatus()->getId()."' ".
                            ", '".$obj->getCreatedBy()->getId()."' ".
                            ", null ".
                            ")"));
            if (!$result) { return null; }
            
            return DB::getPdo()->lastInsertId();
    }

    
    public function updateObject($obj, $old_id){

        DB::update(DB::raw("UPDATE issue SET name='".$obj->getName()."', start_date='".$obj->getStart_date()."', due_date='".$obj->getDue_date()."', priority='".$obj->getPriority()."', task_id=".$obj->getTask_id().", status_id=".$obj->getStatus_id().", created_by='".$obj->getCreated_by()->getId()."', last_updated_by='".$obj->getLast_updated_by()."' WHERE id=".$old_id." "));
    return true;
    }
    
    public function updateStatus($status, $id){
        DB::update(DB::raw("UPDATE issue SET status_id='".$status."' WHERE id=".$id." "));
    return true;
    }
    
    public function deleteObject($id){
        DB::delete(DB::raw("DELETE FROM issue where id='".$id."'"));
        
        return true;
    
    }
    
    public function getObject($id){
        
        $result = DB::select(DB::raw("SELECT * FROM issue where id='".$id."'"));
        
        return $this->toObject($result[0]);
    }
    
    public function getList($criteria = NULL){
        $result = DB::select(DB::raw("SELECT * FROM issue"));
        
        $list = array();
        foreach ($result as $rowdata){
            $list[] = $this->toObject($rowdata);            
        }
        
        return $list;
    }
    
    public function getDetail($id){
        $result = DB::select(DB::raw("SELECT detail FROM issue where id='".$id."'"));
        
        return $result;
    }
    
    public function getListByTaskId($taskId){
        $result = DB::select(DB::raw("SELECT * FROM issue where task_id='".$taskId."'"));
        
        $list = array();
        foreach ($result as $rowdata){
            $list[] = $this->toObject($rowdata);            
        }
        
        return $list;
    }
    
    protected function toObject($rowdata=null){
        $obj = new Issue();
        $obj->setId($rowdata->id);
        $obj->setName($rowdata->name);
        $obj->setDescription($rowdata->description);
        $obj->setStartDate($rowdata->start_date);
        $obj->setDueDate($rowdata->due_date);
        
        $priority = new Priority();
        $priority->setId($rowdata->priority_id);
        $priority->setIsLoaded(false);
        $obj->setPriority($priority);
        
        $task = new Task();
        $task->setId($rowdata->task_id);
        $task->setIsLoaded(false);
        $obj->setTask($task);
        
        $status = new Status();
        $status->setId($rowdata->status_id);
        $status->setIsLoaded(false);
        $obj->setStatus($status);

        $user = new User();
        $user->setId($rowdata->created_by);
        $user->setIsLoaded(false);        
        $obj->setCreatedBy($user);
        
        $obj->setCreatedDate($rowdata->created_date);
        
        $last_user = new User();
        $last_user->setId($rowdata->last_updated_by);
        $last_user->setIsLoaded(false);        
        $obj->setLastUpdateBy($last_user);
        
        $obj->setLastUpdateDate($rowdata->last_updated_date);
        $obj->setIsLoaded(true);
        
        return $obj;
    }
    
}